<template>
  <div class="order">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="功能开发中" class="input-with-select" disabled>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table
        :data="orderList"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单总价" prop="order_price" width="120px"></el-table-column>
        <el-table-column label="是否付款" width="120px">
          <template v-slot="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="120px"></el-table-column>
        <el-table-column label="创建时间" width="200px">
          <template v-slot="scope">
            {{scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="changeCityDialog"
            >编辑地址</el-button>
            <el-button
            type="warning"
            icon="el-icon-location"
            size="mini"
            @click="showLogisticsDialog">物流状态</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="changeCityDialogVisible"
      width="50%"
      @close="changeCityDialogClosed"
    >
      <el-form
        :model="adressForm"
        :rules="adressFormRules"
        ref="adressFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="省市区/县" prop="city_name">
          <el-cascader
            v-model="adressForm.cityAdress"
            :options="citydata"
            :props="{ expandTrigger: 'hover' }"
          >
            </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detail_adress">
          <el-input v-model="adressForm.detailAdress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="changeCityDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeCityDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流查询对话框 -->
    <el-dialog
      title="物流查询"
      :visible.sync="logisticsDialogVisible"
      width="50%"
    >
      <span>功能开发中</span>
    </el-dialog>
  </div>
</template>

<script>
import citydata from './citydata.js'
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 订单列表数据
      orderList: [],
      total: 0,
      // 修改地址对话框显示与隐藏
      changeCityDialogVisible: false,
      // 修改地址表单
      adressForm: {
        cityAdress: [],
        detailAdress: ''
      },
      // 城市列表
      citydata,
      // 修改地址验证
      adressFormRules: {
        city_name: [
          { required: true, message: '请选择省市区、县', trigger: 'blur' }
        ],
        detail_adress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 物流对话框显示与隐藏
      logisticsDialogVisible: false,
      // 物流信息数据列表
      logisticsList: []
    }
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      console.log(res.data)
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 页面总量改变处理函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 页码改变处理函数
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 修改地址处理函数
    changeCityDialog() {
      this.changeCityDialogVisible = true
    },
    // 修改地址对话框处理函数
    changeCityDialogClosed() {
      this.$refs.adressFormRef.resetFields()
    },
    // 物流查询
    showLogisticsDialog() {
      this.logisticsDialogVisible = true
    }
  },
  created() {
    this.getOrderList()
  }
}
</script>

<style lang="less" scoped>
  .el-cascader {
    width: 100%;
  }
</style>
